package com.wsoft.excel.handler;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**
 * 用于 Excel 导出 自定义合并单元格
 * @Author z001
 * @Date 2024/11/6 10:30
 */
public class ExcelMergeHandler extends AbstractMergeStrategy {

    /**
     * 合并坐标集合
     */
    private List<CellRangeAddress> cellRangeAddresses;

    public ExcelMergeHandler(List<CellRangeAddress> cellRangeAddresses) {
        this.cellRangeAddresses = cellRangeAddresses;
    }


    /**
     * 合并操作：对每个单元格执行！！！
     *
     * @param sheet            sheet对象
     * @param cell             当前单元格
     * @param head             表头对象
     * @param relativeRowIndex 相关行索引
     */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        if(CollUtil.isEmpty(cellRangeAddresses)){
            return;
        }
        for (CellRangeAddress item : cellRangeAddresses) {
            sheet.addMergedRegionUnsafe(item);
        }
    }
}
